/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mahumLogs.backedbeans;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

/**
 *
 * @author Kalast
 */

@Entity
public class Log implements Serializable {
    
    @EmbeddedId LogID atTime;
    
    @Column(length = 20)
    private String ip;
    
    @Lob
    private String url;
    
    @Column(length = 100)
    private String domaine;

    @Column(length = 50)
    private String mimeType;
    
    private long requestSize;
    private int requestTime;
    
    private int requestStatus = 0;
    
    @Transient
    private int idEtudiant = -1;
    @Transient
    private String nomEtudiant = "???";
    @Transient
    private String prenomEtudiant = "???";
 
    /**
     * Constructeur
     */
    public Log() {
        
    }

    /**
     * @param id
     * @param domaine
     * @param requestTime
     * @param requestSize
     * @param ip
     * @param url
     * @param requestStatus
     * @param mimetype 
     */
    public Log(LogID id, String domaine, int requestTime, int requestSize, String ip, String url, int requestStatus, String mimetype) {
        this.ip = ip;
        this.atTime = id;
        this.requestSize = requestSize;
        this.requestTime = requestTime;
        this.url = url;
        this.domaine = domaine;
        this.requestStatus = (requestStatus != 404 && requestStatus != 403 ? 200 : requestStatus);
        this.mimeType = mimetype;
    }

    /**
     * 
     * @return atTime
     */
    public LogID getID() {
        return atTime;
    }

    /**
     * 
     * @return domaine
     */
    public String getDomaine() {
        return domaine;
    }

    /**
     * 
     * @param domaine 
     */
    public void setDomaine(String domaine) {
        this.domaine = domaine;
    }

    /**
     * 
     * @return requestStatus
     */
    public int getRequestStatus() {
        return requestStatus;
    }

    /**
     * 
     * @return requestSize
     */
    public long getRequestSize() {
        return requestSize;
    }

    public String getSizeConverted() {
        String[] sufs = new String[]{"o", "Ko", "Mo", "Go"};
        int index = 0;
        double size = this.requestSize;
        DecimalFormat df = new DecimalFormat("#.##");

        while (size >= 1000d) {
            index++;
            size /= 1000d;
        }

        return df.format(size) + " " + sufs[index];
    }
    
    public String getTimeString(){
        Date d = new Date(this.atTime.time);
        DateFormat df = new SimpleDateFormat("H:mm:ss");
        return df.format(d);
    }

    public String getSizeConverted(double size) {
        String[] sufs = new String[]{"o", "Ko", "Mo", "Go"};
        int index = 0;
        DecimalFormat df = new DecimalFormat("#.##");

        while (size >= 1000d) {
            index++;
            size /= 1000d;
        }

        return df.format(size) + " " + sufs[index];
    }

    public String getTimeConverted() {
        String[] sufs = new String[]{"ms", "s", "m"};
        int index = 0;
        float time = this.requestTime;
        DecimalFormat df = new DecimalFormat("#.##");

        if (time >= 1000) {
            time = time / 1000;
            index++;
        }

        if (time >= 1000) {
            time = time / 60;
            index++;
        }

        return df.format(time) + " " + sufs[index];
    }

    public String getTimePerSize() {
        double size = this.requestSize * 1000d;
        double forOneSec = size / this.requestTime;
        return this.getSizeConverted(forOneSec) + "/s";
    }

    public void setRequestSize(long requestSize) {
        this.requestSize = requestSize;
    }

    public int getRequestTime() {
        return requestTime;
    }

    public void setRequestTime(int requestTime) {
        this.requestTime = requestTime;
    }

    public boolean isRequestAccepted() {
        return this.requestStatus != 403 && this.requestStatus != 404;
    }

    public boolean isRequestNotFound() {
        return this.requestStatus == 404;
    }

    public boolean isRequestForbidden() {
        return this.requestStatus == 403;
    }

    public void setRequestStatus(int requestStatus) {
        this.requestStatus = requestStatus;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getMimeType() {
        return mimeType;
    }

    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }

    public long getAtTime() {
        return this.atTime.time;
    }

    public void setAtTime(long atTime) {
        this.atTime.time = atTime;
    }

    public void setID(LogID id) {
        this.atTime = id;
    }

    public int getIdEtudiant() {
        return idEtudiant;
    }

    public void setIdEtudiant(int idEtudiant) {
        this.idEtudiant = idEtudiant;
    }

    public String getNomEtudiant() {
        return nomEtudiant;
    }

    public void setNomEtudiant(String nomEtudiant) {
        this.nomEtudiant = nomEtudiant;
    }

    public String getPrenomEtudiant() {
        return prenomEtudiant;
    }

    public void setPrenomEtudiant(String prenomEtudiant) {
        this.prenomEtudiant = prenomEtudiant;
    }
    
    
}
